import {config} from '../config.js'

class HTTP {
  request({
    url,
    data = {},
    method = 'GET',
    header = {}
  }) {
    return new Promise((resolve, reject) => {

      this._request(url, resolve, reject, data, method, header)
    })
  }
  _request(url, resolve, reject, data, method, header) {
    console.log(data);
    var now_time = new Date().getTime();
    if (JSON.stringify(data) != "{}"){
      data.timestamp = now_time;
      var arr = []
      for (let i in data) {
        arr.push(i + "=" + data[i]);
      }
      if (arr.length > 0) {
        arr.sort();
        var params_s = "";
        for (var i = 0; i < arr.length; i++) {
          params_s += arr[i] + '&';
        }
        params_s = params_s.substring(0, params_s.lastIndexOf("&"));
      }
    }
    
    config.header.token = getApp().globalData.token;
    console.log(config.api_base_url + url)
    // url, data, method
    wx.request({
      url: config.api_base_url + url,
      method: method,
      data: data,
      header: Object.assign(config.header, header),
      success: (res) => {
        console.log(res);
        
        const code = res.data.code;
        const msg = res.data.msg;
        resolve(res.data)

        if (code == 0 || code == 2) {
        
        }
        else if (code == 1){
          if(String(msg) == "登录失效,请您重新登录"){
            wx.showToast({
              title: msg,
              icon:'none'
            })
            setTimeout(function(){
              wx.redirectTo({
                url: '/pages/login/login',
              })
              // wx.navigateTo({
              //   url: '/pages/login/login',
              // })
            },1000)
            wx.removeStorage({
              key: 'userinfo',
              success(res) {
                console.log(res)
              }
            })
            wx.removeStorage({
              key: 'token',
              success(res) {
                console.log(res)
              }
            })
          }
          else{

          }
          
        }else if(code == 4){
          
          if(String(msg) == "当前登录已失效,请重新登录"){
            
            wx.showToast({
              title: msg,
              icon:'none'
            })
            setTimeout(function(){
              wx.redirectTo({
                url: '/pages/login/login',
              })
              // wx.navigateTo({
              //   url: '/pages/login/login',
              // })
            },1000)
            wx.removeStorage({
              key: 'userinfo',
              success(res) {
                console.log(res)
              }
            })
            wx.removeStorage({
              key: 'token',
              success(res) {
                console.log(res)
              }
            })
          }
          else{

          }
        }
        else {
          reject()
          let error_code = res.data.msg;
          
          this._show_error(error_code)
        }
      },
      fail: (err) => {
        console.log(err);
        reject()
        this._show_error(1)
      }
    })
  }

  // 私有方法
  _show_error(error_code) {
    // if (!error_code) {
    //   error_code = 1
    // }
    // const tip = config.tips[error_code]
    wx.showToast({
      title: error_code,
      icon: 'none',
      duration: 2000
    })
  }
}

export {
  HTTP
}